import 'package:flutter/material.dart';
import 'package:flutter_member/widget/user_course_card.dart';
import 'package:parse_server_sdk/parse_server_sdk.dart';
import 'package:toast/toast.dart';

import 'course_details_tab.dart';
class CoursePage extends StatefulWidget {
  final List<dynamic> listCourse;

  const CoursePage({Key key, this.listCourse}) : super(key: key);

  @override
  _CoursePageState createState() => _CoursePageState();
}

class _CoursePageState extends State<CoursePage> {
  /// 初始化方法
  @override
  void initState() {
    super.initState();
    _getCourseClass();
  }

//  頁面銷毀
  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

//  獲取我的課程
  Future<void> _getCourseClass() async {
    final ParseUser user = await ParseUser.currentUser() as ParseUser;
    ParseObject student = ParseObject('Student');
    QueryBuilder queryMember = QueryBuilder(ParseObject('Member'))..whereEqualTo('objectId', user.get('member')['objectId']);
    ParseResponse repStu = await queryMember.query();
    if(repStu.success&&repStu.results!=null){
      student=repStu.results.first['student'];

    }else{
      Toast.show("你還為成為會員", context,
          duration: Toast.LENGTH_SHORT, gravity: Toast.CENTER);
    }
  }

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Column(
        children: widget.listCourse
            .map((f) => UserCourseCard(
                  onPressed: () => Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => CourseDetailsTab())),
                  map: f,
                ))
            .toList(),
      ),
    );
  }
}
